<template>
  <div>
    <div>
      <el-input style="width: 200px;" placeholder="查询模块" v-model="operation"></el-input>
      <el-select placeholder="查询操作类型" style="margin-left: 5px" v-model="type">
        <el-option v-for="item in ['新增','修改','删除','注册','登录']" :key="item" :value="item" :label="item"></el-option>
      </el-select>
      <el-input style="width: 200px;" placeholder="查询操作用户" v-model="operationuser"></el-input>
      <el-button type="primary" style="margin-left: 10px" @click="load(1)" plain>查询</el-button>
      <el-button type="primary" @click="reset" plain>重置</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-button type="danger" plain @click="doubleDelete">批量删除</el-button>
    </div>
    <el-table :data="tableData" stripe :header-cell-style="{backgroundColor:'aliceblue',color:'#666'}"
              @selection-change="handleSelectChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column label="序号" prop="id" width="70"></el-table-column>
      <el-table-column label="操作模块" prop="operation"></el-table-column>
      <el-table-column label="操作类型" prop="type">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.type==='新增'">{{ scope.row.type }}</el-tag>
          <el-tag type="info" v-if="scope.row.type==='修改'">{{ scope.row.type }}</el-tag>
          <el-tag type="danger" v-if="scope.row.type==='删除'">{{ scope.row.type }}</el-tag>
          <el-tag type="danger" v-if="scope.row.type==='批量删除'">{{ scope.row.type }}</el-tag>
          <el-tag type="primary" v-if="scope.row.type==='登录'">{{ scope.row.type }}</el-tag>
          <el-tag type="primary" v-if="scope.row.type==='注册'">{{ scope.row.type }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作人IP" prop="ip"></el-table-column>
      <el-table-column label="操作人" prop="user"></el-table-column>
      <el-table-column label="操作时间" prop="time"></el-table-column>
      <el-table-column label="操作" align="center">
        <template v-slot="scope">
          <div>
            <el-button size="small" type="danger" @click="del(scope.row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-size="pageSize"
          layout="total,prev,pager,next"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "Logs",
  data() {
    return {
      user: JSON.parse(localStorage.getItem('base-user') || '{}'),
      tableData: [],//所有数据
      pageNum: 1,//当前页码
      pageSize: 6,//每页显示个数
      username: '',
      operation: '',
      total: 0,
      form: {},
      ids: [],
      type: '',
      operationuser: '',
    }
  },
  created() {
    this.load()
  },
  methods: {
    doubleDelete() {
      if (!this.ids.length) {
        this.$message.warning("请选择数据")
        return
      }
      this.$confirm("确认批量删除这些数据么", "确认删除", {type: "warning"}).then(response => {
        this.$request.delete('/logs/delete/double', {data: this.ids}).then(res => {
          if (res.code === "200") {//操作成功
            this.$message.success("操作成功")
            this.load(1)
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {
      })
    },
    handleSelectChange(rows) {
      this.ids = rows.map(v => v.id)
    },
    del(id) {
      this.$confirm("确认删除么", "确认删除", {type: "warning"}).then(response => {
        this.$request.delete('/logs/delete/' + id).then(res => {
          if (res.code === "200") {//操作成功
            this.$message.success("操作成功")
            this.load(1)
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {
      })
    },
    reset() {
      this.operationuser = ''
      this.type = ''
      this.operation = ''
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
    load(pageNum) {  //分页查询
      if (pageNum) this.pageNum = pageNum
      this.$request.get('/logs/selectByPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          operation: this.operation,
          type: this.type,
          user: this.operationuser,

        }
      }).then(res => {
        // 检查 res.data 是否为 null 或 undefined
        if (res.data === null || res.data === undefined) {
          // 处理错误情况，例如显示错误消息或清空表格数据
          console.error('未获取到数据');
          this.tableData = [];
          this.total = 0;
        } else {
          // 正常处理数据
          this.tableData = res.data.records;
          this.total = res.data.total;
        }
      }).catch(error => {
        // 处理请求错误
        console.error('请求数据出错:', error);
        // 可以根据需要设置一些错误处理逻辑，比如显示错误消息
      });
    },
  }
}
</script>

<style>
.el-tooltip__popper {
  max-width: 300px !important;
}
</style>